package cn.senjoeson.triper.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;

import org.greenrobot.greendao.query.QueryBuilder;

import java.util.List;

import butterknife.BindView;
import cn.senjoeson.mylibrary.utilschanged.MyToast;
import cn.senjoeson.triper.R;
import cn.senjoeson.triper.adapter.DinningListAdapter;
import cn.senjoeson.triper.base.BaseActivity;
import cn.senjoeson.triper.base.GreenDaoManager;
import cn.senjoeson.triper.bean.FoodDrinkBean;
import cn.senjoeson.triper.bean.FoodDrinksBean;
import cn.senjoeson.triper.bean.Plan;
import cn.senjoeson.triper.dao.FoodDrinkBeanDao;
import cn.senjoeson.triper.dao.PlanDao;
import cn.senjoeson.triper.utils.ReadAssetFileUtils;

public class DinningListActivity extends BaseActivity {


    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.left_arrow)
    ImageView mLeftArrow;
    @BindView(R.id.title)
    TextView mTitle;
    @BindView(R.id.search)
    ImageView mSearch;
    @BindView(R.id.filter)
    ImageView mFilter;
    @BindView(R.id.map)
    ImageView mMap;
    @BindView(R.id.recyclerview)
    RecyclerView mRecyclerview;

    public FoodDrinkBeanDao mFoodDrinkBeanDao;
    private boolean defBoolean;
    public DinningListAdapter mDinningListAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_dinning_list;
    }

    @Override
    protected void initView() {
        setSupportActionBar(mToolbar);
        String title = getIntent().getStringExtra("title");
        mTitle.setText(title);
    }

    @Override
    protected void initData() {
        // ArrayList<FoodDrinkBean> mDatas = new ArrayList<>();
        // Random random = new Random();
        mFoodDrinkBeanDao = GreenDaoManager.getInstance().getNewSession().getFoodDrinkBeanDao();
        /*for (int i = 0; i < 14; i++) {
            FoodDrinkBean foodDrinkBean = new FoodDrinkBean();
            foodDrinkBean.setDinningName("北京某饭店");
            foodDrinkBean.setStar(random.nextInt(5) + 1);
            foodDrinkBean.setFoodrinkId(random.nextLong());
            // mDatas.add(foodDrinkBean);
            mFoodDrinkBeanDao.insert(foodDrinkBean);
        }*/
        String fileContent = ReadAssetFileUtils.readFile(this, "places.json");
        if (fileContent != null) {
            FoodDrinksBean foodDrinksBean = new Gson().fromJson(fileContent, FoodDrinksBean.class);
            List<FoodDrinkBean> foodDrinksBeans = foodDrinksBean.getFoodDrinksBeans();
            for (FoodDrinkBean drinksBean : foodDrinksBeans) {
                if (mFoodDrinkBeanDao.load(drinksBean.getFoodrinkId()) == null) {
                    mFoodDrinkBeanDao.insert(drinksBean);
                }
            }


            mRecyclerview.setLayoutManager(new LinearLayoutManager(this));
            mDinningListAdapter = new DinningListAdapter(this);
            mDinningListAdapter.setDatas(foodDrinksBeans);
            mRecyclerview.setAdapter(mDinningListAdapter);
        }
       /* mDinningListAdapter.setOnItemClickListener(new DinningListAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent = new Intent(AppUtils.getContext(), DestinationActivity.class);
                intent.putExtra("placeId",)
                startActivity(intent);
            }
        });*/

    }

    private List<FoodDrinkBean> getFoodDrinkBeans(int defaultOrder) {
        QueryBuilder<FoodDrinkBean> foodDrinkBeanQueryBuilder = mFoodDrinkBeanDao.queryBuilder();
        switch (defaultOrder) {
            case 0:
                break;
            case 1:
                foodDrinkBeanQueryBuilder.orderAsc(FoodDrinkBeanDao.Properties.Star);
                MyToast.showToast("正序");
                break;
            case 2:
                foodDrinkBeanQueryBuilder.orderDesc(FoodDrinkBeanDao.Properties.Star);
                MyToast.showToast("倒序");
                break;
        }
        return foodDrinkBeanQueryBuilder.build().list();
    }

    @Override
    protected void initListener() {
        mLeftArrow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PlanDao planDao = GreenDaoManager.getInstance().getNewSession().getPlanDao();
                List<Plan> list = planDao.queryBuilder().build().list();
                if (list != null && list.size() > 0) {
                    startActivity(MainActivity.class);
                } else
                    onBackPressed();
            }
        });
        mSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(SearchActivity.class);
            }
        });
        mFilter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<FoodDrinkBean> foodDrinkBeans = null;
                if (!defBoolean) {
                    foodDrinkBeans = getFoodDrinkBeans(1);
                    defBoolean = true;
                } else {
                    foodDrinkBeans = getFoodDrinkBeans(2);
                    defBoolean = false;
                }
                mDinningListAdapter.setDatas(foodDrinkBeans);
                mDinningListAdapter.notifyDataSetChanged();
            }
        });
    }


}
